package com.ky.serialport.service;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.ky.serialport.bean.ComBean;
import com.ky.serialport.helper.ComControl;
import com.ky.serialport.message.AskMsg;
import com.ky.myserialport.R;
import com.ky.serialport.sensor.ChaYaAckBean;
import com.ky.serialport.sensor.ChaYaDataBean;
import com.ky.serialport.sensor.DianYaBianSongQiAckBean;
import com.ky.serialport.sensor.DianYaBianSongQiBean;
import com.ky.serialport.sensor.GongLvDataBean;
import com.ky.serialport.sensor.GuoSuBean;
import com.ky.serialport.sensor.LaLiDataBean;
import com.ky.serialport.sensor.RouXingDianLiuAckBean;
import com.ky.serialport.sensor.RouXingDianLiuDataBean;
import com.ky.serialport.sensor.DianYa24AckBean;
import com.ky.serialport.sensor.DianYa24DataBean;
import com.ky.serialport.sensor.DianYa75AckBean;
import com.ky.serialport.sensor.DianYa75DataBean;
import com.ky.serialport.sensor.DianYaHeAckBean;
import com.ky.serialport.sensor.DianYaHeDataBean;
import com.ky.serialport.sensor.DianYaHeHeartBean;
import com.ky.serialport.sensor.GeBaoKeAckBean;
import com.ky.serialport.sensor.GeBaoKeDataBean;
import com.ky.serialport.sensor.JiaWanDataBean;
import com.ky.serialport.sensor.SpeedAckBean;
import com.ky.serialport.sensor.SpeedDataBean;
import com.ky.serialport.sensor.TimeAckBean;
import com.ky.serialport.sensor.TimeDataBean;
import com.ky.serialport.sensor.TimeHeartBean;
import com.ky.serialport.sensor.WeiYiAckBean;
import com.ky.serialport.sensor.WeiYiDataBean;
import com.ky.serialport.sensor.WenDuBean;
import com.ky.serialport.sensor.WenShiDuDataBean;
import com.ky.serialport.sensor.YaLiAckBean;
import com.ky.serialport.sensor.YaLiDataBean;
import com.ky.serialport.sensor.ZhuanSuDataBean;
import com.ky.serialport.sensor.setting.GongLvSettingBean;
import com.xzkydz.serialport.SerialControl;
import com.ky.serialport.utils.DataType;
import com.ky.serialport.utils.FormatUtils;
import com.ky.serialport.utils.MyFunc;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * 串口解析服务
 */
public class SerialPortService extends Service {

    private String TAG = "串口数据";
    private String contentTitle = "数据接收";
    private String contentText = "传感器数据接收中...";
    private OnGetSerialPortDataThread onGetSerialPortData;
    private OnAddNewSensorType onAddNewSensorType;
    private boolean _IsDefault = true; //是否默认解析
    /*功率箱参数*/
    private float dybbF = 1, dlbbF = 1, qblchF = 500;
    private String cshff = "单瓦特";

    //记录传感器的数据包情况
    private Map<Integer, Integer> wshdMap = new HashMap<>();
    private Map<Integer, Integer> suduDlMap = new HashMap<>();
    private Map<Integer, Integer> suduShLMap = new HashMap<>();
    private Map<Integer, Integer> mv75Map = new HashMap<>();
    private Map<Integer, Integer> rxdlMap = new HashMap<>();
    private Map<Integer, Integer> gbkMap = new HashMap<>();
    private Map<Integer, Integer> weiYiMap = new HashMap<>();
    private Map<Integer, Integer> yaliMap = new HashMap<>();
    private Map<Integer, Integer> yali20Map = new HashMap<>();
    private Map<Integer, Integer> yali60Map = new HashMap<>();
    private Map<Integer, Integer> yali1Map = new HashMap<>();
    private Map<Integer, Integer> yali15Map = new HashMap<>();
    private Map<Integer, Integer> yali10Map = new HashMap<>();
    private Map<Integer, Integer> yali0Map = new HashMap<>();
    private Map<Integer, Integer> yali2000Map = new HashMap<>();
    private Map<Integer, Integer> chaya8Map = new HashMap<>();
    private Map<Integer, Integer> chaya2Map = new HashMap<>();
    private Map<Integer, Integer> dyhMap = new HashMap<>();
    private Map<Integer, Integer> jshqMap = new HashMap<>();
    private Map<Integer, Integer> mv24Map = new HashMap<>();
    private Map<Integer, Integer> lali20Map = new HashMap<>();
    private Map<Integer, Integer> lali40Map = new HashMap<>();
    private Map<Integer, Integer> jiawanMap = new HashMap<>();
    private Map<Integer, Integer> zhuansuMap = new HashMap<>();
    private Map<Integer, Integer> glxMap = new HashMap<>();
    private Map<Integer, Integer> guosuMap = new HashMap<>();


    //串口实例化
    private SerialControl com;
    private boolean _IsReadSerial = false;
    private NotificationManager notificationManager;
    private WenShiDuDataBean wenShiDuDataBean = new WenShiDuDataBean();
    private DianYa75AckBean dianLiu75AckBean = new DianYa75AckBean();
    private DianYa75DataBean dianLiu75DataBean = new DianYa75DataBean();
    private RouXingDianLiuAckBean dianLiuAckBean = new RouXingDianLiuAckBean();
    private RouXingDianLiuDataBean dianLiuDataBean = new RouXingDianLiuDataBean();
    private GeBaoKeAckBean geBaoKeAckBean = new GeBaoKeAckBean();
    private GeBaoKeDataBean geBaoKeDataBean = new GeBaoKeDataBean();
    private SpeedAckBean speedAckBean = new SpeedAckBean();
    private SpeedDataBean speedDataBean = new SpeedDataBean();
    private TimeDataBean timeDataBean = new TimeDataBean();
    private TimeHeartBean timeHeartBean = new TimeHeartBean();
    private TimeAckBean timeAckBean = new TimeAckBean();
    private WeiYiAckBean weiYiAckBean = new WeiYiAckBean();
    private WeiYiDataBean weiYiDataBean = new WeiYiDataBean();
    private LaLiDataBean laLiDataBean = new LaLiDataBean();
    private YaLiAckBean yaLiAckBean = new YaLiAckBean();
    private YaLiDataBean yaLiDataBean = new YaLiDataBean();
    private ChaYaAckBean chaYaAckBean = new ChaYaAckBean();
    private ChaYaDataBean chaYaDataBean8 = new ChaYaDataBean();
    private ChaYaDataBean chaYaDataBean2 = new ChaYaDataBean();
    private JiaWanDataBean jiaWanDataBean = new JiaWanDataBean();
    private ZhuanSuDataBean zhuanSuDataBean = new ZhuanSuDataBean();
    private DianYa24AckBean dianYa24AckBean = new DianYa24AckBean();
    private DianYa24DataBean dianYa24DataBean = new DianYa24DataBean();
    private DianYaHeAckBean dianYaHeAckBean = new DianYaHeAckBean();
    private DianYaHeDataBean dianYaHeDataBean = new DianYaHeDataBean();
    private DianYaHeHeartBean dianYaHeHeartBean = new DianYaHeHeartBean();
    private GongLvDataBean gongLvDataBean = new GongLvDataBean();
    private WenDuBean wenDuBean = new WenDuBean();
    private GuoSuBean guoSuBean = new GuoSuBean();
    private DianYaBianSongQiBean dianYaBianSongQiBean = new DianYaBianSongQiBean();
    private DianYaBianSongQiAckBean dianYaBianSongQiAckBean = new DianYaBianSongQiAckBean();


    @Override
    public void onCreate() {
        super.onCreate();
        FormatUtils.init();
        EventBus.getDefault().register(this);
        com = new SerialControl(this, DataType.DATA_OK_PARSE);
        com.setiDelay(10);
        com.setOpenXor(true);
        ComControl.OpenComPort(com);
    }

    //发送请求命令
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEvent(AskMsg event) {
        if (event.getSendContent() == null || event.getSendContent().length() == 0) {
            com.sendHex("4B590BFFFF00010102FFFF0000E3EA");
        } else if (event.getSendContent() != null && event.getSendContent().length() != 0) {
            com.sendHex(event.getSendContent());
        }
        Log.d(TAG, "发送请求命令: " + event.toString());
    }

    //功率箱配置数据
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getSettingGongLv(GongLvSettingBean event) {
        this.dlbbF = event.getDlbbF();
        this.dybbF = event.getDybbF();
        this.cshff = event.getCshff();
        this.qblchF = event.getQblchF();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 注册通知服务
        notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        Notification.Builder builder = new Notification.Builder(this.getApplicationContext()); //获取一个Notification构造器
        builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.danpianji)) // 设置下拉列表中的图标(大图标)
                .setContentTitle(contentTitle) // 设置下拉列表里的标题
                .setSmallIcon(R.mipmap.danpianji) // 设置状态栏内的小图标
                .setContentText(contentText) // 设置上下文内容
                .setWhen(System.currentTimeMillis()) // 设置该通知发生的时间
                .setVibrate(null)
                .setVibrate(new long[]{0l})
                .setSound(null)
                .setLights(0, 0, 0)
                .setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE);
        Notification notification = builder.build(); // 获取构建好的Notification
        notification.defaults = Notification.FLAG_ONLY_ALERT_ONCE; /*设置为默认的声音*/
        startForeground(110, notification);
        //开启读取线程
        _IsReadSerial = true;

        if (_IsDefault) {
            new ReadDataThread().start();
        } else {
            new ReadDataThread2().start();
        }

        return super.onStartCommand(intent, flags, startId);
    }

    private boolean haveRepetition(Map<Integer, Integer> map, int key, int value) {
        if (map.containsKey(key)) {
            if (map.get(key) == value) {
                return true;
            }
        }
        map.put(key, value);
        return false;
    }

    /*解析串口线程*/
    public class ReadDataThread extends Thread {
        @Override
        public void run() {
            super.run();
            while (_IsReadSerial && !isInterrupted()) {
                try {
                    final ComBean comData;
                    while ((comData = com.QueueList.poll()) != null) {
                        // 接收到数据
                        int type = comData.recData[9];
                        //设备编号
                        int devNum = comData.recData[10];
                        //数据序号
                        int dataNum = MyFunc.twoByte2IntNoSign(comData.recData[11], comData.recData[12]);
                        Log.d(TAG, MyFunc.ByteArrToHex(comData.recData) + "  类型 : " + type + "  长度 : " + comData.recData.length);
                        byte[] buffer = comData.recData;
                        switch (type) {
                            case 16: //温湿度大气压
                                Log.d(TAG, "run: 温湿度大气压");
                                if (haveRepetition(wshdMap, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(wenShiDuDataBean.caculate(comData));
                                break;
                            case 17: // 温度传感器
                                Log.d(TAG, "run: 温度");
                                EventBus.getDefault().post(wenDuBean.caculate(comData));
                                break;
                            case 96: //单轮无线速度传感器
                                Log.d(TAG, "run: 单轮速度" + (buffer.length == 16 ? "ACK" : "数据"));
                                if (buffer.length != 16 && haveRepetition(suduDlMap, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 16 ? speedAckBean.caculate(comData) : speedDataBean.caculate(comData));
                                break;
                            case 98: //双轮无线速度传感器
                                Log.d(TAG, "run: 双轮速度" + (buffer.length == 16 ? "ACK" : "数据"));
                                if (buffer.length != 16 && haveRepetition(suduShLMap, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 16 ? speedAckBean.caculate(comData) : speedDataBean.caculate(comData));
                                break;
                            case -89://0xA7 直流±75mv
                                Log.d(TAG, "run:0xA7 直流±75mv" + (buffer.length == 33 ? "数据" : "ACK"));
                                //因为不同时期的硬件，ACK长度不同，所以判断数据包长度
                                if (buffer.length == 33 && haveRepetition(mv75Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 33 ? dianLiu75DataBean.caculate(comData) : dianLiu75AckBean.caculate(comData));

                                break;
                            case -86: //0xAA无线柔性电流传感器
                                Log.d(TAG, "run: 0xAA无线柔性电流" + (buffer.length == 16 ? "ACK" : "数据"));
                                if (buffer.length != 16 && haveRepetition(rxdlMap, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 16 ? dianLiuAckBean.caculate(comData) : dianLiuDataBean.caculate(comData));
                                break;
                            case -84: //电流电压采集器（隔爆壳）
                                Log.d(TAG, "run: 电流电压采集器（隔爆壳）" + (buffer.length == 16 ? "ACK" : "数据"));
                                if (buffer.length != 16 && haveRepetition(gbkMap, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 16 ? geBaoKeAckBean.caculate(comData) : geBaoKeDataBean.caculate(comData));
                                break;
                            case -90: //0xA6无线位移传感器
                                Log.d(TAG, "run: 0xA6无线位移" + (buffer.length == 16 ? "ACK" : "数据"));
                                if (buffer.length != 16 && haveRepetition(weiYiMap, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 16 ? weiYiAckBean.caculate(comData) : weiYiDataBean.caculate(comData));
                                break;
                            case 32: //0x20  20Mpa 无线压力传感器
                                Log.d(TAG, "run:0x20  20Mpa压力" + (buffer.length == 22 ? "ACK" : "数据"));
                                if (buffer.length != 22 && haveRepetition(yali20Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? new YaLiAckBean().caculate(comData) : new YaLiDataBean().caculate(comData));
                                break;
                            case 33: //0x21  60MPA
                                Log.d(TAG, "run:0x21  60Mpa压力" + (buffer.length == 22 ? "ACK" : "数据"));
                                if (buffer.length != 22 && haveRepetition(yali60Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? new YaLiAckBean().caculate(comData) : new YaLiDataBean().caculate(comData));
                                break;
                            case 35: //0x23  -0.1~ 1 MPa
                                Log.d(TAG, "run:0x23  -0.1~ 1 MPa压力" + (buffer.length == 22 ? "ACK" : "数据"));
                                if (buffer.length != 22 && haveRepetition(yali1Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? new YaLiAckBean().caculate(comData) : new YaLiDataBean().caculate(comData));
                                break;
                            case 36: //0x24  -0.1~1.5
                                Log.d(TAG, "run:0x24  -0.1~1.5 MPa压力" + (buffer.length == 22 ? "ACK" : "数据"));
                                if (buffer.length != 22 && haveRepetition(yali15Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? new YaLiAckBean().caculate(comData) : new YaLiDataBean().caculate(comData));
                                break;
                            case 37: //0x25   -0.2 ~ 10
                                Log.d(TAG, "run:0x25  -0.2 ~ 10 MPa压力" + (buffer.length == 22 ? "ACK" : "数据"));
                                if (haveRepetition(yali10Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? new YaLiAckBean().caculate(comData) : new YaLiDataBean().caculate(comData));
                                break;
                            case 38: //0x26   -1.5 ~ 0 kPa
                                Log.d(TAG, "run:0x26  -1.5 ~ 0 kPa压力" + (buffer.length == 22 ? "ACK" : "数据"));
                                if (buffer.length != 22 && haveRepetition(yali0Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? new YaLiAckBean().caculate(comData) : new YaLiDataBean().caculate(comData));
                                break;
                            case 39: //0x27   0 ~ 2000 hPa  / 10
                                Log.d(TAG, "run:0x27  0 ~ 2000 hPa压力" + (buffer.length == 22 ? "ACK" : "数据"));
                                if (buffer.length != 22 && haveRepetition(yali2000Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? new YaLiAckBean().caculate(comData) : new YaLiDataBean().caculate(comData));
                                break;
                            case 34: //0x22 -8Kpa差压协议
                                Log.d(TAG, "run:0x22 -8Kpa 差压" + (buffer.length == 22 ? "ACK" : "数据"));
                                if (buffer.length != 22 && haveRepetition(chaya8Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? new ChaYaAckBean().caculate(comData) : new ChaYaDataBean().caculate(comData));
                                break;
                            case 48: //-2 ~ 2 kPa 差压
                                Log.d(TAG, "run:-2 ~ 2 kPa 差压" + (buffer.length == 22 ? "ACK" : "数据"));
                                if (buffer.length != 22 && haveRepetition(chaya2Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? new ChaYaAckBean().caculate(comData) : new ChaYaDataBean().caculate(comData));
                                break;
                            case 131: //无线过速
                                break;
                            case 83: //本安信号转换器（电压盒、交直流回路）
                                if (buffer.length == 18) {
                                    Log.d(TAG, "run: 本安信号转换器（电压盒、交直流回路）" + (buffer[13] == 3 ? "心跳" : "ACK"));
                                    EventBus.getDefault().post(buffer[13] == 3 ? dianYaHeHeartBean.caculate(comData) : dianYaHeAckBean.caculate(comData));
                                } else {
                                    /*去掉重复检测*/
//                                    if (haveRepetition(dyhMap, devNum, dataNum)) {
//                                        Log.d(TAG, "run: 重复包");
//                                        break;
//                                    }
                                    Log.d(TAG, "run: 本安信号转换器（电压盒、交直流回路）数据");
                                    EventBus.getDefault().post(dianYaHeDataBean.caculate(comData));
                                }
                                break;
                            case 81: //0x51 无线计时器
                                if (buffer.length == 18) {
                                    Log.d(TAG, "run: 0x51 计时器" + (buffer[13] == 3 ? "心跳包" : "ACK"));
                                    // buffer[13] 03是心跳包，04是ACK
                                    EventBus.getDefault().post(buffer[13] == 3 ? timeHeartBean.caculate(comData) : timeAckBean.caculate(comData));
                                } else {
                                    /*计时器比较特殊，去掉重复包检测*/
//                                    if (haveRepetition(gbkMap, devNum, dataNum)) {
//                                        Log.d(TAG, "run: 重复包");
//                                        break;
//                                    }
                                    Log.d(TAG, "run: 0x51 计时器数据");
                                    EventBus.getDefault().post(timeDataBean.caculate(comData));
                                }
                                break;
                            case -79: //0xb1 直流24mv
                                Log.d(TAG, "run: 0xb1 直流24mv" + ((buffer.length == 22 ? "ACK" : "数据包")));
                                if (buffer.length != 22 && haveRepetition(mv24Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(buffer.length == 22 ? dianYa24AckBean.caculate(comData) : dianYa24DataBean.caculate(comData));
                                break;
                            case 64: // 20T 拉力
                                Log.d(TAG, "run: 20T 拉力");
                                if (haveRepetition(lali20Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(laLiDataBean.caculate(comData));
                            case 66: // 40T 拉力
                                Log.d(TAG, "run: 40T 拉力");
                                if (haveRepetition(lali40Map, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(laLiDataBean.caculate(comData));
                                break;
                            case -78: //甲烷
                                Log.d(TAG, "run: 甲烷");
                                if (haveRepetition(jiawanMap, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(jiaWanDataBean.caculate(comData));
                                break;
                            case 97: //转速
                                Log.d(TAG, "run: 转速");
                                if (haveRepetition(zhuansuMap, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(zhuanSuDataBean.caculate(comData));
                                break;
                            case -128: //功率箱
                                Log.d(TAG, "run: 功率箱");
                                if (haveRepetition(glxMap, devNum, dataNum)) {
                                    Log.d(TAG, "run: 重复包");
                                    break;
                                }
                                EventBus.getDefault().post(gongLvDataBean.caculate(buffer, dybbF, dlbbF, qblchF, cshff));
                                break;
                            case -82: //过速
                                switch (comData.recData.length) {
                                    case 16:
                                        break;
                                    case 22:
                                        Log.d(TAG, "run: 过速传感器");
                                        if (haveRepetition(guosuMap, devNum, dataNum)) {
                                            Log.d(TAG, "run: 过速重复包");
                                        }
                                        EventBus.getDefault().post(guoSuBean.caculate(comData));
                                        break;
                                }
                                break;
                            case  -81: // GYD50W矿用本安型无线电压变送器
                                Log.d(TAG, "run: GYD50W矿用本安型无线电压变送器");
                                switch (comData.recData.length){
                                    case 38: //数据包长度
                                        EventBus.getDefault().post(dianYaBianSongQiBean.caculate(comData));
                                        break;
                                    case 26: //ACK响应包
                                        EventBus.getDefault().post(dianYaBianSongQiAckBean);
                                        break;
                                }
                                break;
                            default:
                                if (null != onAddNewSensorType) {
                                    onAddNewSensorType.onGetData(comData, buffer, type);
                                }
                                break;
                        }
                        try {
                            Thread.sleep(10);//显示性能高的话，可以把此数值调小。
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                } catch (Exception e) {
                    Log.d(TAG, "run: 解析串口数据的时候产生异常" + e.getMessage());
                }
            }
        }
    }


    /*解析串口线程*/
    public class ReadDataThread2 extends Thread {
        @Override
        public void run() {
            super.run();
            while (_IsReadSerial && !isInterrupted()) {
                try {
                    final ComBean comData;
                    while ((comData = com.QueueList.poll()) != null) {
                        // 接收到数据
                        onGetSerialPortData.onGetData(comData.recData, comData.recData[9], comData.recTime);
                        try {
                            Thread.sleep(10);//显示性能高的话，可以把此数值调小。
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                    }
                } catch (Exception e) {
                    Log.d(TAG, "run: 解析串口数据的时候产生异常" + e.getMessage());
                }
            }
        }
    }

    public void setContentTitle(String contentTitle) {
        this.contentTitle = contentTitle;
    }

    public void setContentText(String contentText) {
        this.contentText = contentText;
    }

    public void set_IsDefault(boolean _IsDefault) {
        this._IsDefault = _IsDefault;
    }

    //添加新的传感器解析
    public interface OnAddNewSensorType {
        public void onGetData(ComBean comData, byte[] dataArray, int sensorType);
    }

    //自己解析
    public interface OnGetSerialPortDataThread {
        public void onGetData(byte[] dataArray, int sensorType, Long time);
    }


    @Override
    public void onDestroy() {
        _IsReadSerial = false;
        ComControl.CloseComPort(com);
        stopForeground(true);// 停止前台服务--参数：表示是否移除之前的通知
        notificationManager.cancel(110);
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
